package com.xindz.hgq.main;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.logger.Logger;
import com.xindz.hgq.R;
import com.xindz.hgq.app.AppConstants;
import com.xindz.hgq.base.BaseFragment;
import com.xindz.hgq.base.options.BaseFragmentOptions;
import com.xindz.hgq.database.greendao.view.GdDataCacheActivity;
import com.xindz.hgq.database.greendao.view.GdRelationalExampleActivity;
import com.xindz.hgq.database.greendao.view.GdSimplyExampleActivity;
import com.xindz.hgq.database.greendao.view.GdV2Activity;
import com.xindz.hgq.database.realm.view.RealmSimpleExampleActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.realm.ObjectServerError;
import io.realm.SyncCredentials;
import io.realm.SyncUser;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.main
 * @describe describe
 * @anthor XINDZ
 * @time 2018/5/14 16:35
 * @change
 * @chang time
 * @class describe
 */
public class DatabaseFragment extends BaseFragment {
    @BindView(R.id.bt_gd_init)
    Button btGdInit;
    @BindView(R.id.bt_gd_simply_example)
    Button btGdSimplyExample;
    Unbinder unbinder;
    @BindView(R.id.bt_gd_data_cache)
    Button btGdDataCache;
    @BindView(R.id.bt_gd_data_relation)
    Button btGdDataRelation;
    @BindView(R.id.bt_rm_cloud_sign)
    Button btRmCloudSign;
    @BindView(R.id.bt_rm_cloud_logout)
    Button btRmCloudLogout;
    @BindView(R.id.bt_gd_v2)
    Button btGdV2;
    @BindView(R.id.bt_rm_simply_example)
    Button btRmSimplyExample;
    private ProgressDialog progressDialog;

    @Override
    public BaseFragmentOptions setOption() {
        return null;
    }

    @Override
    public int onCreateFragmentLayoutId() {
        return R.layout.fragment_datebase;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.bt_gd_init, R.id.bt_gd_simply_example, R.id.bt_gd_data_cache, R.id.bt_gd_data_relation,R.id.bt_gd_v2
            , R.id.bt_rm_cloud_sign, R.id.bt_rm_cloud_logout, R.id.bt_rm_simply_example})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_gd_init:
                break;
            case R.id.bt_gd_simply_example:
                ActivityUtils.startActivity(GdSimplyExampleActivity.class);
                break;
            case R.id.bt_gd_data_cache:
                ActivityUtils.startActivity(GdDataCacheActivity.class);
                break;
            case R.id.bt_gd_data_relation:
                ActivityUtils.startActivity(GdRelationalExampleActivity.class);
                break;
            case R.id.bt_gd_v2:
                ActivityUtils.startActivity(GdV2Activity.class);
                break;
            case R.id.bt_rm_cloud_sign:
                realmCloudSign();
                break;
            case R.id.bt_rm_cloud_logout:
                realmCloudLogout();
                break;
            case R.id.bt_rm_simply_example:
                ActivityUtils.startActivity(RealmSimpleExampleActivity.class);
                break;
        }
    }

    public void realmCloudSign() {
        progressDialog = ProgressDialog.show(getActivity(), "realmCloud登录", null, true, false);
        //登录成功后 SyncUser 将被持久化到本地,不用重新登录
        if (null != SyncUser.current()) {
            progressDialog.dismiss();
            ToastUtils.showShort("已经登录成功");
        } else {
            SyncCredentials credentials = SyncCredentials.nickname("17610956106", false);
            SyncUser.logInAsync(credentials, AppConstants.AUTH_URL, new SyncUser.Callback<SyncUser>() {
                @Override
                public void onSuccess(SyncUser user) {
                    Logger.i("realmCloudSign--onSuccess:" + user.toJson());
                    progressDialog.dismiss();
                    ToastUtils.showShort("登录成功");
                }

                @Override
                public void onError(ObjectServerError error) {
                    Logger.i("realmCloudSign--onError:" + error.getErrorMessage());
                    progressDialog.dismiss();
                    ToastUtils.showShort("登录失败");
                }
            });
        }

    }

    /**
     * 注销
     */
    public void realmCloudLogout() {
        if (null != SyncUser.current()) {
            SyncUser.current().logOut();
            ToastUtils.showShort("已经退出登录");
        } else {
            ToastUtils.showShort("用户未登录登录");
        }
    }

    @OnClick(R.id.bt_gd_v2)
    public void onViewClicked() {
    }
}
